package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.NursingLevelDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @author maochuan
 * 护理等级
 */
@RequestMapping("NursingLevel")
@RestController
public class NursingLevelController extends BaseController {

    @Autowired
    @Qualifier("NursingLevelServiceImplOne")
    private NursingLevelService nursingLevelService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("NursingItemServiceImplOne")
    private NursingItemService nursingItemService;

    @Autowired
    @Qualifier("NursingLevelItemRelServiceImplOne")
    private NursingLevelItemRelService nursingLevelItemRelService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;


    //列表
    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int status = jsonObject.getInteger("status");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/NursingLevel/Page");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<NursingLevel> pageInfoNursingLevel = nursingLevelService.getPageInfo(pageIndex, pageSize, institutionCode,name,status);
            List<NursingLevelDTO> nursingLevelDTOList = new ArrayList<>();
            for(NursingLevel nursingLevel : pageInfoNursingLevel.getList()){
                NursingLevelDTO nursingLevelDTO = mapper.map(nursingLevel,NursingLevelDTO.class);
                nursingLevelDTOList.add(nursingLevelDTO);
            }
            for (NursingLevelDTO nursingLevelDTO:nursingLevelDTOList){
                List<NursingLevelItemRel> nursingLevelItemRelList = nursingLevelItemRelService.getByNursingLevelCode(nursingLevelDTO.getNursingLevelCode());
                List<NursingItem> nursingItemList = new ArrayList<>();
                for(NursingLevelItemRel nursingLevelItemRel:nursingLevelItemRelList){
                    NursingItem nursingItem = nursingItemService.getByNursingItemCode(nursingLevelItemRel.getNursingItemCode());
                    nursingItemList.add(nursingItem);
                }
                nursingLevelDTO.setNursingItemList(nursingItemList);
            }
            pageInfoNursingLevel.setList(Collections.unmodifiableList(nursingLevelDTOList));

            return success(pageInfoNursingLevel);

        } catch (Exception e) {
            insertError("/NursingLevel/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //列表
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int status = jsonObject.getInteger("status");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/NursingLevel/Page");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<NursingLevel> pageInfoNursingLevel = nursingLevelService.pageByGovCode(pageIndex, pageSize, staff.getGovernmentCode());
            List<NursingLevelDTO> nursingLevelDTOList = new ArrayList<>();
            for(NursingLevel nursingLevel : pageInfoNursingLevel.getList()){
                NursingLevelDTO nursingLevelDTO = mapper.map(nursingLevel,NursingLevelDTO.class);
                nursingLevelDTOList.add(nursingLevelDTO);
            }
            for (NursingLevelDTO nursingLevelDTO:nursingLevelDTOList){
                List<NursingLevelItemRel> nursingLevelItemRelList = nursingLevelItemRelService.getByNursingLevelCode(nursingLevelDTO.getNursingLevelCode());
                List<NursingItem> nursingItemList = new ArrayList<>();
                for(NursingLevelItemRel nursingLevelItemRel:nursingLevelItemRelList){
                    NursingItem nursingItem = nursingItemService.getByNursingItemCode(nursingLevelItemRel.getNursingItemCode());
                    nursingItemList.add(nursingItem);
                }
                nursingLevelDTO.setNursingItemList(nursingItemList);
            }
            pageInfoNursingLevel.setList(Collections.unmodifiableList(nursingLevelDTOList));

            return success(pageInfoNursingLevel);

        } catch (Exception e) {
            insertError("/NursingLevel/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    /**
     * 查询所有状态为0的护理等级
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult list(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/NursingLevel/List");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();
            List<NursingLevel> nursingLevelList =nursingLevelService.getList(institutionCode);
            return success(nursingLevelList);

        } catch (Exception e) {
            insertError("/NursingLevel/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //添加
    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            double price = jsonObject.getDouble("price");
            JSONArray itemCodeArr = jsonObject.getJSONArray("itemCodeArr");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/NursingLevel/Add");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();


            NursingLevel nursingLevel = nursingLevelService.getByNameAndInstitutionCode(name,institutionCode);
            if (nursingLevel != null) {
                return failed("该护理等级已存在");
            }
            if(itemCodeArr.size()==0||itemCodeArr==null||itemCodeArr.equals("null")){
                return failed("没有选择护理项目");
            }
            nursingLevel = new NursingLevel();
            nursingLevel.setNursingLevelCode(UUID.randomUUID().toString());
            nursingLevel.setName(name);
            nursingLevel.setSort(sort);
            nursingLevel.setPriceMonth(price);
            nursingLevel.setStaffCode(staffCode);
            nursingLevel.setInstitutionCode(institutionCode);
            nursingLevelService.insertLeveLAndItem(nursingLevel,itemCodeArr);

            Object object = new Object[]{};
            return success(object);

        } catch (Exception e) {
            insertError("/NursingLevel/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //编辑
    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String nursingLevelCode = jsonObject.getString("nursingLevelCode");
            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            int status = jsonObject.getInteger("status");
            double price = jsonObject.getDouble("price");
            JSONArray itemCodeArr = jsonObject.getJSONArray("itemCodeArr");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/NursingLevel/Edit");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();


            NursingLevel nursingLevel = nursingLevelService.getByNameAndInstitutionCode(name,institutionCode);
            if (nursingLevel != null) {
                if(!nursingLevel.getNursingLevelCode().equals(nursingLevelCode)){
                    return failed("该护理等级已存在");
                }
            }
            nursingLevel = nursingLevelService.getByNursingLevelCode(nursingLevelCode);
            nursingLevel.setName(name);
            nursingLevel.setSort(sort);
            nursingLevel.setStatus(status);
            nursingLevel.setPriceMonth(price);

            nursingLevelService.updateByNursingLevelAndNursingItem(nursingLevel,itemCodeArr);

            Object object = new Object[]{};
            return success(object);


        } catch (Exception e) {
            insertError("/NursingLevel/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


}
